package team.se.tms.controller.student;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import team.se.tms.annotation.CheckFormat;
import team.se.tms.annotation.Regex;
import team.se.tms.annotation.SpecialCheck;
import team.se.tms.constant.SessionConstant;
import team.se.tms.controller.BaseController;
import team.se.tms.dto.Page;
import team.se.tms.dto.StudentBasicDetail;
import team.se.tms.dto.TeachCourseProjectDetail;
import team.se.tms.enums.DateEnum;
import team.se.tms.exception.InsertException;
import team.se.tms.exception.ParamException;
import team.se.tms.exception.SelectException;
import team.se.tms.exception.UpdateException;
import team.se.tms.po.SelectCourseInfo;
import team.se.tms.po.StudentInfo;
import team.se.tms.po.TeacherInfo;
import team.se.tms.po.UserInfo;
import team.se.tms.service.StudentInfoService;
import team.se.tms.service.TeacherInfoService;
import team.se.tms.util.Jurisdiction;
import team.se.tms.util.ResultUtil;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("student")
public class StudentInfoController extends BaseController{
    @Autowired
    StudentInfoService studentInfoService;

    @Autowired
    TeacherInfoService teacherInfoService;
//测试
    @RequestMapping("toView")
    public String searchStudent() throws Exception{
        return "student/student_list";
    }
    /**
     * 查询学生单个信息
     *
     * @param stuId 学号
     * @return ModelAndView
     * @throws Exception 异常
     * @create on 2019/1/3 by sunddly
     */
    @RequestMapping("viewStudentDetail")
    public ModelAndView viewStudentDetail(@Regex("^[A-Za-z0-9]{10}$") String stuId, HttpSession session) throws Exception{
        ModelAndView mv = new ModelAndView();
        StudentBasicDetail studentBasicDetail = new StudentBasicDetail();
        List<StudentBasicDetail> studentBasicDetails =(List<StudentBasicDetail>)session.getAttribute("studentBasicDetails");
        for (StudentBasicDetail stu:studentBasicDetails){
            if (stu.getStuId().equals(stuId)){
                studentBasicDetail = stu;
            }
        }
        mv.addObject("studentBasicDetail",studentBasicDetail);
        mv.setViewName("student/student_info_details");
        return mv;
    }
    /**
     * 查询学生基本信息
     *
     * @return result
     * @throws Exception 异常
     * @create on 2019/1/2 by sunddly
     */
    @RequestMapping("info")
    public ModelAndView viewStudentBasicInfo() throws Exception{
        ModelAndView mv = this.getModelAndView();
        String userId= ((UserInfo) Jurisdiction.getSession().getAttribute(SessionConstant.USER)).getUserId();
        StudentBasicDetail studentBasicDetail = studentInfoService.getStudentBasic(userId);
        mv.addObject("studentBasicDetail",studentBasicDetail);
        mv.setViewName("student/stu_my_info");
        return mv;
    }
    /**
     * 查询学生的所有授课的项目详情
     *
     * @param studentId 学生Id
     * @param page
     * @return String
     * @throws Exception 异常
     * @create on 2019/1/2 by sunddly
     */
    @RequestMapping("teachCourseProjectList")
    @ResponseBody
    public String searchTeachCoureProject(String studentId,Page page) throws Exception{
        List<TeachCourseProjectDetail> teachCourseProjectDetailList = null;
        teachCourseProjectDetailList =studentInfoService.listTeachCourseProjcet(studentId,page);
        System.out.println("&&&&&&&&&&&&&&&"+teachCourseProjectDetailList);
        return page.getDataTabelJson(teachCourseProjectDetailList, DateEnum.ACCURATE_TO_DAY);
    }
    /**
     * 去查询该项目的所有作业信息
     *
     * @param projectId 项目ID
     * @return result
     * @throws Exception 异常
     * @create on 2019/1/2 by sunddly
     */
    @RequestMapping("viewProjectAssignments")
    public ModelAndView viewProjectAssignments(String projectId) throws Exception{
        ModelAndView mv = this.getModelAndView();
       // List<StudentInfo> groupStudentInfoList =studentInfoService.listGroupStudentInfos(projectId);
        mv.addObject("projectId",projectId);
        //mv.addObject("groupStudentInfoList",groupStudentInfoList);
        System.out.println("()()()()()()()()()");
        mv.setViewName("assignment/project_assignment_list");
        return mv;
    }

    /**
     * 查询学生单个信息
     *
     * @return
     * @throws Exception
     * @create on 2018/12/28 by qingx
     */
    @RequestMapping(value = "searchStudent",method = RequestMethod.POST)
    @ResponseBody
    @CheckFormat({"selectCourseGrade","selectCourseJudgement"})
    public Object searchStudentInfo(SelectCourseInfo selectCourseInfo) throws Exception{
        System.out.println("111111111selectCourseInfo"+selectCourseInfo);
        StudentInfo stu = studentInfoService.getStudentInfo(selectCourseInfo);
        System.out.println("111111111stu"+stu);
        if(stu != null){
            return  ResultUtil.success("查询成功",stu);
        }
        throw new SelectException("学号错误");
    }

    @RequestMapping("list")
    public String list() throws Exception{
        return "student/student_list";
    }

    /**
     * 查询学生列表
     *
     * @param page
     * @return
     * @throws Exception
     * @create on 2018/12/29 by qingx
     */
    @RequestMapping("getList")
    @ResponseBody
    public String searchAllStudentBasicDetails(Page page,HttpSession httpSession) throws Exception{
        System.out.println("page.getOrderDir()"+page.getOrderDir());
        List<StudentBasicDetail> studentBasicDetails =  studentInfoService.listAllStudentBasicDetails(page);
        httpSession.setAttribute("studentBasicDetails",studentBasicDetails);
        System.out.println("page.getDataTabelJson"+page.getDataTabelJson(studentBasicDetails));
        return page.getDataTabelJson(studentBasicDetails);
    }

    /**
     * 跳转到添加学生页面
     *
     * @return
     * @throws Exception
     * @create on 2018/12/29 by qingx
     */
    @RequestMapping("toAddStudent")
    public ModelAndView searchAllTeacherInfos() throws Exception{
        ModelAndView mv = this.getModelAndView();
        List<TeacherInfo> teacherInfos = teacherInfoService.listAllTeacherInfos(new Page());
        System.out.println("!!!!!!!!!!!!!!!!!!"+teacherInfos);
        //将教师信息返回给界面
        mv.addObject("teacherInfos",teacherInfos);
        mv.setViewName("student/add_student");
        return mv;
    }

    /**
     * 添加学生信息
     *
     * @param addType 添加学生类型
     * @param studentInfo 学生信息
     * @return
     * @throws Exception
     * @create on 2018/12/29 by qingx
     */
    @RequestMapping(value = "addStudentInfo",method = RequestMethod.POST)
    @CheckFormat({"stuFailNumber","userId"})
    @ResponseBody
    public Object addStudentInfo(String addType,StudentInfo studentInfo) throws Exception{
        System.out.println("11111111111111111"+studentInfo);
        String userIdReturn = studentInfoService.savaStudentInfo(addType,studentInfo);
        if(userIdReturn == null){
            throw new InsertException("学生录入失败");
        }else if (userIdReturn != null && addType.equals("addNewUser")){
            return  ResultUtil.success("学生录入成功，用户id为"+userIdReturn);
        }else if (userIdReturn != null && addType.equals("addExistingUser")){
            return  ResultUtil.success("学生录入成功");
        }
        throw new InsertException("学生录入失败");
    }

    /**
     * 跳转到修改学生页面
     *
     * @return
     * @throws Exception
     * @create on 2018/12/29 by qingx
     */
    @RequestMapping("toModityStudent")
    public ModelAndView viewStudentInfo(@Regex("^[A-Za-z0-9]{16}$") String stuId,HttpSession session) throws Exception{
        ModelAndView mv = this.getModelAndView();
        List<StudentBasicDetail> studentBasicDetails = (List<StudentBasicDetail>)session.getAttribute("studentBasicDetails");
        StudentBasicDetail studentBasicDetail = new StudentBasicDetail();
        for(StudentBasicDetail item:studentBasicDetails){
            if(item.getStuId().equals(stuId)){
                studentBasicDetail = item;
                break;
            }
        }
        System.out.println("!!!!!!!!!!!!!!!!!!"+studentBasicDetail);
        //将学生信息返回给界面
        mv.addObject("studentBasicDetail",studentBasicDetail);
        List<TeacherInfo> teacherInfos = teacherInfoService.listAllTeacherInfos(new Page());
        System.out.println("!!!!!!!!!!!!!!!!!!"+teacherInfos);
        //将教师信息返回给界面
        mv.addObject("teacherInfos",teacherInfos);
        mv.setViewName("student/modify_student");
        return mv;
    }

    /**
     * 修改学生信息
     *
     * @param studentInfo 学生信息
     * @return
     * @throws Exception
     * @create on 2018/12/29 by qingx
     */
    @RequestMapping(value = "editStudentInfo",method = RequestMethod.POST)
    // @CheckFormat({"labelId"})
    @ResponseBody
    @CheckFormat
    public Object editStudentInfo(StudentInfo studentInfo) throws Exception{
        System.out.println("111111111111111111"+studentInfo);
        boolean result = studentInfoService.modifyStudentInfo(studentInfo);
        if(result == true){
            return  ResultUtil.success("学生信息修改成功");
        }
        throw new UpdateException("学生信息修改失败");
    }


    /**
     * 批量上传学生信息
     *
     * @return json字符串
     * @throws Exception 异常
     * @create on 2018/12/30 by qingx
     */
    @RequestMapping(value="/batch_upload_student")
    @ResponseBody
    public Object addStudentInfos(@RequestParam("file")MultipartFile multipartFile) throws Exception {
        if(multipartFile==null){
            throw new ParamException("文件不能为空");
        }
        log.debug("上传了{},类型{}", multipartFile.getOriginalFilename(),multipartFile.getContentType());
        boolean result = studentInfoService.saveStudentInfos(multipartFile);
        if (!result){
            throw new InsertException("批量上传学生失败");
        }
        return ResultUtil.success("批量上传学生成功");
    }

    /**
     * 批量修改学生挂科数
     *
     * @return json字符串
     * @throws Exception 异常
     * @create on 2018/12/31 by qingx
     */
        @RequestMapping(value="/batch_upload_student_fail_number")
        @ResponseBody
        public Object editStudentFailNums(@RequestParam("modifyFile")MultipartFile multipartFile) throws Exception {
            if(multipartFile==null){
                throw new ParamException("文件不能为空");
            }
            log.debug("上传了{},类型{}", multipartFile.getOriginalFilename(),multipartFile.getContentType());
            boolean result = studentInfoService.modifyStudentFailNums(multipartFile);
            if (result == true){
                return ResultUtil.success("挂科数修改成功");
            }
            throw new InsertException("挂科数修改失败");
        }
}
